package study.lxy.spring.springboot2.controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import study.lxy.spring.springboot2.entity.MyResponseEntity;
import study.lxy.spring.springboot2.entity.User;
import study.lxy.spring.springboot2.service.UserService;
import study.lxy.spring.springboot2.utils.consts.ErrorCode;
import study.lxy.spring.springboot2.utils.exception.BussiException;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value="通过ID获取用户", notes="")
    @GetMapping(value = "/user/{id}")
    public MyResponseEntity getUser(@PathVariable String id){
        User user = userService.queryUser(id);
        return MyResponseEntity.success(user);
    }
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public MyResponseEntity createUser(@RequestBody User user){
        userService.createUser(user);
        return MyResponseEntity.success("创建用户成功");
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public MyResponseEntity listUser(@RequestParam int pageNum,@RequestParam int pageSize){

        List<User> userList = userService.queryByPage(pageNum,pageSize);
        return MyResponseEntity.success(userList);

    }

    @ApiOperation(value="测试方法", notes="单纯的测试方法")
    @GetMapping("/test")
    public String test() {
        return "test";
    }

}
